package br.com.tooist.pong2;

import android.app.Activity;
import android.os.Bundle;
// get screen size
import android.view.Display;

// treeeedeee
import android.opengl.GLSurfaceView;
// full screen
import android.view.Window;
import android.view.WindowManager;
// multi touch
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import android.widget.ImageView;
import android.widget.TextView;

import android.util.Log;

// menu
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;



// player struct
class Pong2Player {
	// current down pointer ID on the screen
	public int pointer;
	// NOTE: why those are floats?!
	public float pointerX;
	public float pointerY;
	// current handle position
	public int handlePos;
	// current pad position
	public int padPos;
	// current pad size
	public int padSize;
	// current score
	public int score;
	// player color
	public int color;
	//public boolean isComputer;
}


// most of what is down here is copy/past from the multitouch example

public class Pong2 extends Activity implements OnTouchListener {
	private static final String TAG = "PONG2" ;

	private Pong2Player p1;
	private Pong2Player p2;

	private boolean paused;

	// used to calculate touch areas
	private int screenW;
	private int screenH;
	// where the top screen ends and the middle of the screen starts
	private int middleScreenTop;
	// where the bottom screen ends and the middle of the screen starts
	private int middleScreenBottom;

	private GLSurfaceView mGLSurfaceView;
	//
	// Called when the activity is first created.
	@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
		paused = true;
		// full screen
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// deal with screen sizes
		Display display = getWindowManager().getDefaultDisplay();
		this.screenW = display.getWidth();
		this.screenH = display.getHeight();
		int rotation = display.getRotation();
		float ratio = (float) this.screenW / (float) this.screenH;
		StringBuilder sb = new StringBuilder();
		sb.append("WINDOW SIZE: " );
		sb.append( this.screenW );
		sb.append("x" );
		sb.append( this.screenH );
		sb.append(" rotation:" );
		sb.append( rotation );
		sb.append(" ratio" );
		sb.append( ratio );
		Log.d(TAG, sb.toString());

		if( rotation != 0 ){
			Log.d(TAG, "WARNING: for some reason we did get a screen rotated");
			// do something to 'unrotate' it and draw along the vertical size
		}
		// TODO: maintain a 0.6 ratio for the game play...
		
		// calculate where the player touch areas are
		this.middleScreenTop = (this.screenH / 3) + 1;
		this.middleScreenBottom = this.middleScreenTop * 2;
		sb = new StringBuilder();
		sb.append("neutral middle area begins: " );
		sb.append( this.middleScreenTop );
		sb.append(" ends: " );
		sb.append( this.middleScreenBottom );
		Log.d(TAG, sb.toString());

		// instantiate our GL view. TODO: create a splash view, menu view and game view.
		// for now we will start directly into game mode
		mGLSurfaceView = new GLSurfaceView(this);
		mGLSurfaceView.setRenderer(new Pong2Renderer());
		setContentView(mGLSurfaceView);
		mGLSurfaceView.setOnTouchListener(this);
		// TODO: show main menu
		// old 2D code
		//setContentView(R.layout.main);
		//ImageView view = (ImageView) findViewById(R.id.imgGame);
		//view.setOnTouchListener(this);
		// Initialize players
		p1 = new Pong2Player();
		p2 = new Pong2Player();
	}

	@Override public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
	@Override public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			case R.id.new_game:
				return true;
			case R.id.quit:
				//System.exit();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	private void pause(){
		Log.d(TAG, "pause");
		this.paused = true;
	}
	private void resume(){
		Log.d(TAG, "resume");
		this.paused = false;
	}
	// NOTE: unusual, but we will use this 'loop' as the main game loop. call the rendering method before exit.
	//       since the game will pause when there's no input and resume on new input, that will work out perfectly
	@Override public boolean onTouch(View v, MotionEvent event) {
		// Dump touch event to log
		dumpEvent(event);

		// event is for player 1 if in 1/3 top screen, for player two if 1/3 bottom screen.
		// condition 1: no pointer PAUSE
		// condition 2: 3+ pointers PAUSE
		// condition 3: one pointer PAUSE
		// condition 4: two pointers in the same side
		// condition 5: pointer in the middle area
		// condition 6: two pointers, one in each side
		if( event.getPointerCount() != 2 ){
			// condition 1 or 2 or 3 match
			pause();
		}else{
			int t0 = event.getPointerId(0);
			float y0 = event.getY(0);
			int t1 = event.getPointerId(1);
			float y1 = event.getY(1);
			// puts player 1 on top, it may be either pointerIds
			if( y0 <= y1 ){
				p1.pointer = t0;
				p1.pointerY = y0;
				p1.pointerX = event.getX(0);
				p2.pointer = t1;
				p2.pointerY = y1;
				p2.pointerX = event.getX(1);
			}else{
				p2.pointer = t0;
				p2.pointerY = y0;
				p2.pointerX = event.getX(0);
				p1.pointer = t1;
				p1.pointerY = y1;
				p1.pointerX = event.getX(1);
			}

		}

		return true; // indicate event was handled
	}

	// Show an event in the LogCat view, for debugging
	private void dumpEvent(MotionEvent event) {
		String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
			"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
		StringBuilder sb = new StringBuilder();
		int action = event.getAction(); // 0=down, 1=up, 2=move
		int actionCode = action & MotionEvent.ACTION_MASK;

		//TextView changeLabel = (TextView)findViewById(R.id.txtDebug);
		//changeLabel.setText( Integer.toString(action) );

		sb.append("event ACTION_" ).append(names[actionCode]);
		if (actionCode == MotionEvent.ACTION_POINTER_DOWN
				|| actionCode == MotionEvent.ACTION_POINTER_UP) {
			sb.append("(pid " ).append(
						action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
			sb.append(")" );
				}
		sb.append("[" );
		for (int i = 0; i < event.getPointerCount(); i++) {
			sb.append("#" ).append(i);
			sb.append("(pid " ).append(event.getPointerId(i));
			sb.append(")=" ).append((int) event.getX(i));
			sb.append("," ).append((int) event.getY(i));
			if (i + 1 < event.getPointerCount())
				sb.append(";" );
		}
		sb.append("]" );
		Log.d(TAG, sb.toString());
	}
}
